package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.base.ResultMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.service.ScheduleTaskService;
import com.symone.webcms.model.ScheduleTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *   @description : 任务管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-04-04
 */
@RestController
@Api(value="/ScheduleTask", description="任务 管理")
@RequestMapping("/api/task")
public class ScheduleTaskController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(ScheduleTaskController.class);

    @Autowired
    public ScheduleTaskService scheduleTaskService;

    /**
     * @description : 获取任务分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-04
     */
    @RequiresPermissions("sys:task:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value="获取任务分页列表", notes="获取任务分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageSize",value="页大小",required=true,defaultValue = "10"),
            @ApiImplicitParam(name="pageNum",value="页数",required=true,defaultValue = "1")
    })
    public ResultMessage page(Integer pageSize,Integer pageNum) {
        Wrapper<ScheduleTask> ew = new EntityWrapper<ScheduleTask>();
        ew.where("deleted=0");

        //logger.debug("SQL:"+ew.originalSql());

        Page<ScheduleTask> page = new Page<ScheduleTask>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = scheduleTaskService.findPage(page, ew);

        return ResultMessage.success("获取任务的分页列表",page.getRecords(),page.getTotal());
    }

    /**
     * @description : 获取所有任务记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-04
     */
    @RequiresPermissions("sys:task:index")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有任务信息", notes="获取所有任务信息")
    public ResultMessage list() {
        return ResultMessage.success("获取所有任务信息",scheduleTaskService.findAll());
    }

    /**
     * @description : 通过id获取任务
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-04
     */
    @RequiresPermissions("sys:task:get")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value="获取任务信息", notes="获取任务信息")
    @ApiImplicitParam(name="id",value="任务ID",required=true)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取任务信息成功!",scheduleTaskService.selectById(id));
    }

    /**
     * @description : 添加任务
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-04
     */
    @RequiresPermissions("sys:task:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value="添加任务信息", notes="添加任务信息")
    @ApiImplicitParam(name = "scheduleTask", value = "任务信息", required = true )
    public ResultMessage add(ScheduleTask scheduleTask){
        try {
            scheduleTask.setCreateUser(getSessionUser().getUserName());
            if(!scheduleTaskService.addJob(scheduleTask)){
                return ResultMessage.fail("添加任务失败!");
            }
        } catch (SchedulerException e) {
            //e.printStackTrace();
            return ResultMessage.fail(e);
        }
        return ResultMessage.success("添加任务成功!");
    }

    /**
     * @description : 通过id逻辑删除任务
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-04
     */
    @RequiresPermissions("sys:task:remove_flag")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    @ApiOperation(value="逻辑删除任务", notes="逻辑删除任务")
    @ApiImplicitParam(name = "id", value = "任务Id", required = true )
    public ResultMessage removeFlag(Long id){
        if(scheduleTaskService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑任务删除成功！");
        } else {
            return ResultMessage.fail("逻辑任务删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除任务
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-04
     */
    @RequiresPermissions("sys:task:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ApiOperation(value="物理删除任务", notes="物理删除任务")
    @ApiImplicitParam(name = "id", value = "任务Id", required = true )
    public ResultMessage remove(long id){
        if(scheduleTaskService.deleteJob(id)) {
            return ResultMessage.success("物理删除任务成功！");
        } else {
            return ResultMessage.fail("物理删除任务失败！");
        }
    }

    /**
     * @description : 更新任务信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-04
     */
    @RequiresPermissions("sys:task:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value="更新任务信息", notes="更新任务信息")
    @ApiImplicitParam(name = "scheduleTask", value = "任务信息", required = true )
    public ResultMessage edit(ScheduleTask scheduleTask) {
        try {
            scheduleTask.setUpdateUser(getSessionUser().getUserName());
            scheduleTask.setUpdateTime(new Date());
            if(scheduleTaskService.addJob(scheduleTask)) {
                return ResultMessage.success("任务信息更新成功!");
            }else{
                return ResultMessage.fail("任务信息更新失败!");
            }
        } catch (SchedulerException e) {
            return ResultMessage.fail(e);
        }
    }

    /**
     * @description : 更新任务信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-04
     */
    @RequiresPermissions("sys:task:edit")
    @RequestMapping(value = "/status", method = RequestMethod.POST)
    @ApiOperation(value="更新任务信息", notes="更新任务信息")
    @ApiImplicitParam(name = "scheduleTask", value = "任务信息", required = true )
    public ResultMessage status(Long id,String status) {
        ScheduleTask scheduleTask = scheduleTaskService.selectById(id);
        scheduleTask.setStatus(status);
        scheduleTask.setUpdateUser(getSessionUser().getUserName());
        scheduleTask.setUpdateTime(new Date());
        boolean result = false;
        switch (status){
            case ScheduleTask.STATUS_RUNNING:
                result = scheduleTaskService.resumeJob(scheduleTask);
                break;
            case ScheduleTask.STATUS_NOT_RUNNING:
                result = scheduleTaskService.pauseJob(scheduleTask);
                break;
        }
        if(result) {
            return ResultMessage.success("任务信息更新成功!");
        }else{
            return ResultMessage.fail("任务信息更新失败!");
        }
    }

    /**
    * @description : 清除任务记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-04
    */
    @RequiresPermissions("sys:task:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    @ApiOperation(value="清除任务记录", notes="清除已被逻辑删除的任务记录")
    public ResultMessage clear(){
        int num = scheduleTaskService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有任务记录被清除！");
        }
    }



}
